#include <bits/stdc++.h>
#define INF 0x7f7f7f7f
using namespace std;
int heap_size=0,heap[1000010],n,op,number;
void swap(int &a,int &b){int t;t=a;a=b;b=t;}
void heap_put(int num){
	int son,father;
	heap[++heap_size]=num;
	son=heap_size;
	while(son>1){
		father=son>>1;
		if(heap[son]>=heap[father])break;
		swap(heap[son],heap[father]);
		son=father;
	}
}
int heap_print(){
	int son,father=1,res=heap[1];
	heap[1]=heap[heap_size--];
	while(father*2<=heap_size){
		son=father*2;
		if(son<heap_size&&heap[son+1]<heap[son])son++;
		if(heap[father]<=heap[son])break;
		swap(heap[son],heap[father]);
		father=son;
	}
	return res;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>op;
		if(op==1){
			cin>>number;
			heap_put(number);
		}
		else if(op==2){
			cout<<heap[1]<<endl;
		}
		else if(op==3){
			heap_print();
		}
	}
	return 0;
}